package com.luhy.service;

import com.luhy.mapper.OrderMapper;
import com.luhy.pojo.Order;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.lang.model.element.NestingKind;
import java.time.LocalDate;
import java.util.List;
import java.util.UUID;

@Service
public class OrderService {

    private static final Logger log = LoggerFactory.getLogger(OrderService.class);

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public Order insert(Order order) {
        String orderNo = System.currentTimeMillis()+"";
        order.setOrderNo(orderNo);
        order.setStatus(0);
        orderMapper.insert(order);
        rabbitTemplate.send("et", "kt", new Message(orderNo.getBytes()), null);
        return order;
    }

    public List<Order> selectAll() {
        return orderMapper.selectAll();
    }

    public void pay(String orderNo) {
        Order order = orderMapper.selectByOrderNo(orderNo);
        if (order.getStatus() != 0) {
            log.error("订单不能进行支付");
        }
        orderMapper.updateStatus(orderNo, 2);
    }

    public void updateStatus(String orderNo, int status) {
        orderMapper.updateStatus(orderNo, status);
    }

    public Order selectByOrderNo(String orderNo) {
        return orderMapper.selectByOrderNo(orderNo);
    }
}
